package query_understanding.query_understand.tree_progress;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.util.CoreMap;
import query_understanding.query_language.doc_entity_tree.DocumentQueryTree;
import query_understanding.query_language.doc_entity_tree.EntityQueryTree;
import query_understanding.query_language.doc_entity_tree.QueryNode;
import query_understanding.query_language.doc_entity_tree.QueryNodeTokenMap;
import semantic_compute.entity_linking.schema.EntityLinkingResult;

import java.util.*;

/**
 * Created by julianzliu on 4/22/2017.
 */
public class DocumentQueryProgress {

    /*
    文档查询可能包含多个句子
    每个句子对应一个 ->  EntityQueryProgress
     */

    //private YagoQueryEntityLinker entityLinker;
    //private TextAnnotator textAnnotator;


    public String originalWholeQuery;
    public DocumentQueryTree documentQueryTree;


    @JsonIgnore
    public Annotation document;
    public EntityLinkingResult entityLinkingResult;

    public Map<CoreMap, Map<EntityLinkingResult.LinkedEntity, QueryNodeTokenMap> > sentEntityTokenMap;
    public Map<CoreMap, Map<String, IndexedWord> >  sentConceptTokenMap;

    /*
    文档查询  包含  多个实体查询
     */
    public List<EntityQueryProgress> entityQueryProgressList;


    private ObjectMapper mapper;



    public DocumentQueryProgress(){
        //this.textAnnotator = textAnnotator;
        //this.entityLinker = entityLinker;
        this.entityQueryProgressList = new ArrayList<>();
        this.mapper = new ObjectMapper();
    }

    public JsonNode genAllSentenceCanditateTrees(){
        ObjectNode docTree = this.mapper.createObjectNode();
        docTree.put("query", this.originalWholeQuery );
        for(EntityQueryProgress progress: this.entityQueryProgressList){
            JsonNode entityTree = progress.genCanditateTrees();
            String hash_code = new Integer(progress.hashCode()).toString();
            docTree.put( hash_code , entityTree );
        }
        return docTree;
    }

    public JsonNode genDocumentTreeJson(){
        ObjectNode docTree = this.mapper.createObjectNode();
        docTree.put("query", this.originalWholeQuery );
        for(EntityQueryTree tree : this.documentQueryTree.entityQueryTreeList ){
            ObjectNode treeNode = this.mapper.createObjectNode();
            treeNode.put("score", tree.treeScore );
            JsonNode entityTree = tree.genDirectedJsonTree( tree.answerEntity );
            treeNode.put("tree", entityTree);
            treeNode.put("query", tree.query);
            treeNode.put("finish", tree.finished);

            String hash_code = new Integer(tree.hashCode()).toString();
            docTree.put( hash_code , treeNode );
        }
        return docTree;
    }

}

